package com.freeseed.Controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.freeseed.model.service.IUserService;
import com.freeseed.util.MailFormatCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("users")
public class RegisterUserController extends ApiController {
    @Autowired
    private IUserService userService;

    @PostMapping("register")
    public R register(String email, String password, String role){
        if(MailFormatCheckUtil.checkEmailFormat(email)){
            try{
                log.info("email: {} | password: {} | role: {}",email,password,role);
                return success(userService.register(email, password, role));
            }catch (Exception e){
                return failed(e.getMessage());
            }
        }
        return failed("invalid email format");
    }
}
